package com.itcodai.utils.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 集合流练习
 * @author c-fanqj
 * @date 2022/5/15
 * @apiNote
 */
public class StreamExercise {
    public static void main(String[] args) {
        //0、实例化集合，存储学生对象
        ArrayList<Student> list = new ArrayList<>();
        Collections.addAll(list,
                new Student("xiaoming", 89),
                new Student("xiaobai", 98),
                new Student("xiaohei", 78),
                new Student("xiaolv", 86),
                new Student("xiaowang", 59),
                new Student("xiaoxiao", 100)
        );
        //1、所有及格的学生信息
        /*list.stream()
                .filter(s -> s.getScore() >= 60)
                .forEach(System.out::println);*/

        //2. 所有及格的学生姓名
        /*list.stream()
                .filter(s -> s.getScore() >= 60)
                .map(Student::getName)
                .forEach(System.out::println);*/

        //3. 所有学生的平均成绩
        double average = list.stream()
                .mapToInt(Student::getScore)
                .average()
                .getAsDouble();
        //System.out.println(average);

        //4. 班级的前3名(按照成绩)
        List<Student> result = list.stream()
                .sorted((s1, s2) -> s2.getScore() - s1.getScore())
                .limit(3)
                .collect(Collectors.toList());
        //result.forEach(System.out::println);

        //5. 班级的3-10名(按照成绩)
        List<Student> result2 = list.stream()
                .sorted((s1, s2) -> s2.getScore() - s1.getScore())
                .limit(5)
                .skip(2)
                .collect(Collectors.toList());
        //result2.forEach(System.out::println);

        //6. 所有不不及格的学生平均成绩
        double average2 = list.stream()
                .filter(s -> s.getScore() < 60)
                .mapToInt(Student::getScore)
                .average()
                .getAsDouble();
        //System.out.println(average2);

        //7. 将及格的学生, 按照成绩降序输出所有信息
        /*list.stream()
                .filter(s->s.getScore()>=60)
                .sorted((s1,s2)->s2.getScore()-s1.getScore())
                .forEach(System.out::println);*/

        //8. 班级学生的总分
        Object sum = list.stream()
                .mapToInt(Student::getScore)
                //.sum()
                .average()
                .getAsDouble()
                ;
        System.out.println(sum);

    }

    static class Student {
        private String name;
        private int score;

        public String getName() {
            return name;
        }

        public int getScore() {
            return score;
        }

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
           /*return "Student{" +
                   "name='" + name + '\'' +
                   ", age=" + age +
                   '}';*/
            return String.format("姓名：%s，成绩：%d", name, score);
        }
    }

}
